clc;
clear all;
close all;

%% Load input image for edge detection algorithms
imgRGB = imread('retina4.jpg');
img = rgb2gray(imgRGB);
[rows cols] = size(img);

%% Matched Filtering
%% Adapt sigma
sigmas = [1 2 3 4];
hsizes = ceil(3 * sigmas);
% thresh = 0.4;
group_num = 12;
figure;
for i = 1:length(sigmas)
    match_edge_img = ecen5283_edge_detect_matched(img, sigmas(i), hsizes(i), group_num);
    subplot(2,2,i);
    imshow(match_edge_img);
    title([' sigma =', num2str(sigmas(i)), ' ; size = ', num2str(2*hsizes(i)+1)]);
end

%% The best
sigmas = [2];
hsizes = ceil(3 * sigmas);
% thresh = 0.4;
group_num = 12;

figure;
handle = tight_subplot(3,2,[.05 .01],[.03 .01],[.01 .01]); axes(handle(1));imshow(imgRGB);title('Original color image');
axes(handle(2));imshow(img);title('Gray image');

for i = 1:length(sigmas)
    [match_edge_img fuse_img thresh_img] = ecen5283_edge_detect_matched(img, sigmas(i), hsizes(i), group_num);    
    axes(handle(3)); imshow(fuse_img,[]);title('Filtered image by matched filter'); 
    axes(handle(4));imshow(thresh_img); title('Thresholded image');
    axes(handle(5));imshow(match_edge_img);
    title([' sigma =', num2str(sigmas(i)), ' ; size = ', num2str(2*hsizes(i)+1)]);    
    
    %superimposed
    axes(handle(6));
    match_rgb = repmat(uint8(255*match_edge_img),[1 1 3]);
    si_img = imadd(imgRGB, match_rgb);
    imshow(si_img);title('Superimposed image');
end